﻿Imports System.Windows.Forms
Imports System.Drawing

Public Class BarGraph
    Inherits Control
    Private M_Border As Integer = 5
    Private M_Space As Integer = 5
    Private M_value As Integer = 5
    Private M_Range1 As Integer = 50
    Private M_Range2 As Integer = 80
    Private M_R1Off As Color = Color.DarkGreen
    Private M_R2Off As Color = Color.Orange
    Private M_R3Off As Color = Color.DarkRed
    Private M_R1On As Color = Color.YellowGreen
    Private M_R2On As Color = Color.Yellow
    Private M_R3On As Color = Color.Red
    Private OldVal As Integer = 0
    Property Range1OnColor() As Color
        Get
            Return M_R1On
        End Get
        Set(ByVal value As Color)
            M_R1On = value
            Me.Refresh()
        End Set
    End Property
    Property Range1OffColor() As Color
        Get
            Return M_R1Off
        End Get
        Set(ByVal value As Color)
            M_R1Off = value
            Me.Refresh()
        End Set
    End Property
    Property Range2OnColor() As Color
        Get
            Return M_R2On
        End Get
        Set(ByVal value As Color)
            M_R2On = value
            Me.Refresh()
        End Set
    End Property
    Property Range2OffColor() As Color
        Get
            Return M_R2Off
        End Get
        Set(ByVal value As Color)
            M_R2Off = value
            Me.Refresh()
        End Set
    End Property
    Property Range3OnColor() As Color
        Get
            Return M_R3On
        End Get
        Set(ByVal value As Color)
            M_R3On = value
            Me.Refresh()
        End Set
    End Property
    Property Range3OffColor() As Color
        Get
            Return M_R3Off
        End Get
        Set(ByVal value As Color)
            M_R3Off = value
            Me.Refresh()
        End Set
    End Property
    Property Range1Max() As Integer
        Get
            Return M_Range1
        End Get
        Set(ByVal value As Integer)
            M_Range1 = value
            Me.Refresh()
        End Set
    End Property
    Property Range2Max() As Integer
        Get
            Return M_Range2
        End Get
        Set(ByVal value As Integer)
            M_Range2 = value
            Me.Refresh()
        End Set
    End Property
    <System.ComponentModel.Category("Appearance")> _
    <System.ComponentModel.DefaultValue(5)> _
    Property BorderWidth() As Integer
        Get
            Return M_Border
        End Get
        Set(ByVal value As Integer)
            M_Border = value
            Me.Refresh()
        End Set
    End Property
    <System.ComponentModel.Category("Appearance")> _
    <System.ComponentModel.DefaultValue(5)> _
    Public Property Spacing() As Integer
        Get
            Return M_Space
        End Get
        Set(ByVal value As Integer)
            M_Space = value
            Me.Refresh()
        End Set
    End Property
    Public Property Value() As Integer
        Get
            Return M_value
        End Get
        Set(ByVal value As Integer)
            If value <> OldVal Then
                M_value = value
                OldVal = value
                Me.Refresh()
            End If
        End Set
    End Property
    Private Sub BarGraph_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim W As Integer
        Dim H As Integer
        Dim EH As Integer
        Dim S As Integer = M_Space
        Dim G As Graphics = e.Graphics

        G.Clear(Color.Black)

        W = Me.Width - (M_Border * 2) - 0
        H = Me.Height - (M_Border * 2) - 0
        EH = (H / 10) - S
        For c = 0 To 9
            Dim R As New Rectangle(M_Border, M_Border + (EH * c) + (S * c), W, EH)

            If (10 - c) < ((M_Range1 + 10) / 10) Then
                If M_value >= (100 - ((c + 0) * 10)) Then
                    Dim BR As New SolidBrush(M_R1On)
                    G.FillRectangle(BR, R)
                Else
                    Dim BR As New SolidBrush(M_R1Off)
                    G.FillRectangle(BR, R)
                End If
            End If

            If (10 - c) < ((M_Range2 + 10) / 10) And (10 - c) >= ((M_Range1 + 10) / 10) Then
                If M_value >= (100 - ((c + 0) * 10)) Then
                    Dim BR As New SolidBrush(M_R2On)
                    G.FillRectangle(BR, R)
                Else
                    Dim BR As New SolidBrush(M_R2Off)
                    G.FillRectangle(BR, R)
                End If
            End If

            If (10 - c) >= ((M_Range2 + 10) / 10) Then
                If M_value >= (100 - ((c + 0) * 10)) Then
                    Dim BR As New SolidBrush(M_R3On)
                    G.FillRectangle(BR, R)
                Else
                    Dim BR As New SolidBrush(M_R3Off)
                    G.FillRectangle(BR, R)
                End If
            End If
        Next
    End Sub

    Public Sub New()
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.UpdateStyles()
    End Sub
End Class
